// pages/my/bjCurrency/rechargeForward/rechargeForward.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneyType:0, // 0:支付 || 1:提现
    moneyCount:null,
    data:{},
    money:'',
    quickList:[{
      title:"5",
      number:5,
    },{
      title:"10",
      number:10,
    },{
      title:"100",
      number:100,
    },{
      title:"500",
      number:500,
    },{
      title:"1000",
      number:1000,
    },]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this;
    if (options.moneyType) {
      self.setData({
        moneyType:options.moneyType,
        
      })
    }
    if (options.money){
      self.setData({
        money: options.money
      })
    }
    if (options.moneyType == 1)
    {
      app.util.ajax({
        url:'api/user/withdrawApplication'
      },res=>{
          
          self.setData({
            data: res.data.data
          }) 
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 更换金额
  changeMoney:function(e){
    let value = e.detail.value;
      this.setData({
        moneyCount: value
      })  
  },
  //失去焦点
  blurMoney:function(e){
    let value = e.detail.value;
    if(value<=0)
    {
      this.setData({
        moneyCount:1
      })
    }
  },
  translate:function(){
    this.setData({
      moneyCount: Number(this.data.money)
    })
  },
  // 快捷
  changeQuickMoney:function(e){
    let value = e.currentTarget.dataset.number;
    this.setData({
      moneyCount:value
    })
  },
  // 提交提现充值
  submitMoney:function(){
    let type = this.data.moneyType,
        _this = this;
    console.log(type)
    if(type==0)
    {
      app.util.ajax({
        url: 'api/User/recharge',
        data: {
          account: _this.data.moneyCount
        }
      }, function (res) {
        let data = res.data.data;
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success(res) {
            app.util.pageJump("/pages/my/bjCurrency/bjCurrency", "redirectTo");
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: 'none'
            })
          }
        })
      })
    }else if(type==1)
    {
      if (_this.data.moneyCount<=0)
      {
        wx.showToast({
          title: '提现金额不能小于1元',
          icon:'none'
        }) 
        return;     
      }else{
      app.util.ajax(
        { url: 'api/user/withdrawApplication',
          method:'POST',
          data:{
            money: _this.data.moneyCount,
            token: app.globalData.token
          }
         },function(res){
           console.log(res)
           wx.showToast({
             title: '提现成功',
           })
           setTimeout(function(){
              wx.switchTab({
                url: "/pages/my/my",
              })
           },2000)
         }) 

      }
    }
   
  }
})